package com.sh.ssao.lottery.config.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * ********************************
 *
 * @author zhaoqingwei.
 * 天下英雄出我辈，一入江湖岁月催.
 * 20182018/4/2016:27
 * ********************************
 * 描述：String 数据处理工具包
 * <p>
 * 注释：
 **/
public class StrUtil {

    /**
     * 生成 ID
     * @param prefix ID前缀(大写字母标记)
     * @param rl 0 左补齐 1 右补齐
     * @param batchno 当前数据库最大ID
     * @param addition 补齐内容
     * @param batchnoLength 固定长度
     * @return
     */
    public static String formatId(String prefix,String rl,String batchno,String addition,int batchnoLength){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuffer strno = new StringBuffer(prefix);
        String temp = "";
        try {
            if (null==batchno ||"".equals(batchno)){
                for (int i = 0; i < batchnoLength; i++) {
                    temp+=addition;
                }
            }else {
                temp = String.valueOf(Integer.parseInt(batchno.substring(batchno.length()-batchnoLength,batchno.length()))+1);
                if ("0".equals(rl)){
                    String _addition = "";
                    for (int i = 0;i<batchnoLength-temp.length();i++){
                        _addition += addition;
                    }
                    temp=_addition+temp;
                }else if ("1".equals(rl)){
                    String _addition = "";
                    for (int i = 0;i<batchnoLength-temp.length();i++){
                        _addition += addition;
                    }
                    temp=temp+_addition;
                }else{
                    temp = "";
                }
            }

            return new String(strno.append(sdf.format(new Date())).append(temp));
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 判断字符串是否为空
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str) {
        boolean b = false;
        if (str != null && str.length() > 0 && str.trim().length() > 0){
            b = true;
        }
        return b;
    }

    /**
     * 带标志id
     * @param mark
     * @return
     */
    public static String generatorId(String mark){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return mark + sdf.format(new Date()) + String.format("%04d",new Random().nextInt(9999));
    }

    /**
     * 带标志id
     * @param mark
     * @return
     */
    public static String generatorId(String mark,int i){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return mark + sdf.format(new Date()) + String.format("%04d",new Random().nextInt(9999))+i;
    }

    /**
     * 不带标志id
     * pattern : yyyyMMddHHmmss
     * @return
     */
    public static String generatorId(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date()) + String.format("%04d",new Random().nextInt(9999));
    }

    /**
     * 获取年月日时分秒字符串
     * pattern : yyyyMMddHHmmss
     * @return
     */
    public static String generatorDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }

    /**
     * 获取年月日时分秒字符串
     * pattern : yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static String getFormatDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    //自定义格式时间

    /**
     * 字符串时间格式化--字符串
     * @param date
     * @param pattern
     * @return
     */
    public static String generatorDate(String date,String pattern){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据时间获取对应星期【1，2，3，4，5，6，7】
     * @param date
     * @return
     */
    public static int dateToWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //因为数组下标从0开始，而返回的是数组的内容，是数组{1,2,3,4,5,6,7}中用1~7来表示，所以要减1
        int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week == 0) {
            week = 7;
        }
        return week;
    }

    /**
     * 字符串时间格式化为时间--date
     * @param date
     * @param pattern
     * @return
     */
    public static Date patternDate(String date,String pattern){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * date时间格式化为时间--date
     * @param date
     * @param pattern
     * @return
     */
    public static Date patternDate1(Date date,String pattern){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间格式化为字符串
     * @param date
     * @param pattern
     * @return
     */
    public static String patternDate(Date date,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     *
     * 获取当前格式化时间
     * @param pattern
     * @return
     *
     */
    public static Date patternDate(String pattern){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * 获取当前格式化时间
     * @param pattern
     * @return
     *
     */
    public static String patternStrting(String pattern){

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    /**
     * 重组数组为字符串.
     * @param strings
     * @return
     */
    public static String spiltType(String[] strings){
        String type = "";
        int i = 0;;
        for (String s:strings) {
            if (null!=s&&!"".equals(s.trim())){
                if (i<strings.length-1){
                    type+=s+",";
                }else {
                    type+=s;
                }
            }
            i++;
        }
        if ("".equals(type.trim())){

            return null;
        }else{
            return type;
        }

    }

    /**
     * 获取当前时间的的格式化字符串
     * @param formatStr
     * @return
     */
    public static String simpleDate(String formatStr){
         DateFormat timeformat = new SimpleDateFormat(formatStr);
         return timeformat.format(new Date());
    }

    /**
     * 计算给定时间，i天前的日期  当前时间的昨天
     * @param date
     * @param i
     * @return
     */
    public static String getXDateBeforeStr(String date ,int i){

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = simpleDateFormat.parse(date);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(parse);

            int d = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE,d-i);
            date = simpleDateFormat.format(calendar.getTime());

            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 计算给定时间，i天前的日期
     * @param date
     * @param i
     * @return
     */
    public static Date getXDateBeforDate(Date date ,int i){
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            int d = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE,d-i);

            return simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 计算给定月份，获取月份几个月前或后的月份，负数向前推移，整数向后退役（-1：前一个月）
     * @param date
     * @param i
     * @return
     */
    public static String getXDateMath(Date date ,int i){
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, i);
            Date m = calendar.getTime();
            String mon = simpleDateFormat.format(m);

            return mon;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取时间月份
     * @param date
     * @return
     */
    public static String getMonth(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
        String format = simpleDateFormat.format(date);

        System.out.println(format);
        return format;
    }


    /**
     * 明天
     * @param date
     * @param i
     * @return
     */
    public static Date getXDateAfterDate(Date date ,int i){
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            int d = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE,d+i);

            return simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        //String xDateBefore = getXDateBeforeStr("2022-11-20 11:37:00", 2);
        //System.out.println(dateToWeek(patternDate("2022-05-10","yyyy-MM-dd")));
        //System.out.println(getMonth(patternDate("2022-1","yyyy-MM")));
    }
}